﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using Script.Net.Scripts;


namespace Script.Net.WebForms
{
	public class ScriptPlacement : Control
	{
		public bool IncludeScriptsFromScriptManager
		{
			get { return (bool)(ViewState["IncludeScriptsFromScriptManager"] ?? false); }
			set { ViewState["IncludeScriptsFromScriptManager"] = value; }
		}



		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (IncludeScriptsFromScriptManager == true)
			{
				ScriptManager mgr = ScriptManager.GetCurrent(this.Page);
				if (mgr != null)
				{
					foreach (var script in mgr.Scripts)
					{
						ScriptFactory.Repository.CreateScript(new ScriptFile
						{
							Path = script.Path
						});
					}
				}
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			var gen = new ScriptGenerator();
			writer.Write(gen.Generate());
		}
	}
}
